package taskTracker.ui.lwuit;

import taskTracker.Task;
import taskTracker.util.Helper;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.layouts.BoxLayout;

/**
 * A form used to add/edit tasks;
 * @author aseldawy
 *
 */
public class TaskForm extends Form {

	public Command cmdOk = new Command("OK");
	public Command cmdCancel = new Command("Cancel");
	private TextField txtName = new TextField();
	private TextArea txtNotes = new TextArea();
	private TextField txtDueTo = new TextField();
	/**Checkbox for whether this is trackable or not*/
	private CheckBox chkTrackable = new CheckBox("Trackable");
	/**Text area that holds tags*/
	private TextArea txtTags = new TextArea();
	/**The task currently being edited/created */
	private Task task;
	
	public TaskForm() {
		super.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		super.addCommand(cmdOk);
		super.addCommand(cmdCancel);
		Label label;
		super.addComponent(label = new Label("Name"));
		super.addComponent(txtName);
		txtName.setLabelForComponent(label);
		
		super.addComponent(label = new Label("Notes"));
		super.addComponent(txtNotes);
		txtNotes.setLabelForComponent(label);
		
		super.addComponent(label = new Label("Due To"));
		super.addComponent(txtDueTo);
		txtDueTo.setLabelForComponent(label);
		
		super.addComponent(chkTrackable);

		super.addComponent(label = new Label("Tags"));
		super.addComponent(txtTags);
		txtTags.setLabelForComponent(label);
		
		super.layoutContainer();
		txtName.setFocus(true);
	}

	public void setTask(Task task) {
		if (task == null) {
			super.setTitle("New Task");
			txtName.setText("");
			txtNotes.setText("");
			txtTags.setText("");
			txtDueTo.setText(Helper.dateToString(System.currentTimeMillis()));
			chkTrackable.setSelected(false);
		} else {
			super.setTitle(task.getName());
			txtName.setText(task.getName());
			txtNotes.setText(task.getNotes());
			txtTags.setText(task.getTagsAsString());
			txtDueTo.setText(Helper.dateToString(task.getDueTo()));
			chkTrackable.setSelected(task.isTrackable());
		}
		this.task = task;
	}

	/**
	 * Retrieves data of task stored in this dialog
	 * @param taskList
	 * @return
	 */
	public Task getTask() {
		if (task == null)
			task = new Task(txtName.getText(), txtNotes.getText());
		task.setName(txtName.getText());
		task.setNotes(txtNotes.getText());
		task.setDueTo(Helper.decodeDate(txtDueTo.getText()));
		task.setTrackable(chkTrackable.isSelected());
		task.setTags(txtTags.getText());
		return task;
	}
}
